package com.example.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

//@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //Web权限方案二(不常用): 重写WebSecurityConfigurerAdapter中的configure方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //对密码进行加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //返回值为加密后的密码
        String password = passwordEncoder.encode("123");

        auth.inMemoryAuthentication()
                .withUser("lucy") //用户名
                .password(password) //密码
                .roles("admin"); //角色
    }

    //在加密密码的过程中,需要用到PasswordEncoder接口,所以需要创建
    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }
}
